/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.preferences.pages;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.toedo.gui.translations.Messages;
import org.toedo.preferences.PreferenceConstants;
import org.toedo.Activator;

public class GeneralPage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {
	
	BooleanFieldEditor mShowInTray;
	BooleanFieldEditor mMinimizeToTray;

	public GeneralPage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription(Messages.GeneralPage_Description);
	}
	
	protected void initialize() {
		super.initialize();
		setAllEnableValue(mShowInTray.getBooleanValue());
	}
	
	public void setAllEnableValue(boolean value) {
		mMinimizeToTray.setEnabled(value, getFieldEditorParent());
	}
	
	public void propertyChange(PropertyChangeEvent event) {		
		if (event.getSource().equals(mShowInTray)) {
			setAllEnableValue(mShowInTray.getBooleanValue());
		}
	}
	
	@Override
	protected void createFieldEditors() {
		
		Label sectionLabel;		
		sectionLabel = new Label(getFieldEditorParent(), SWT.NONE);
		sectionLabel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1));
		
		addField(new BooleanFieldEditor(PreferenceConstants.OPEN_LAST_PROJECT_AT_STARTUP,
				Messages.GeneralPage_OpenLastProject,
				getFieldEditorParent()));
		
		addField(new BooleanFieldEditor(PreferenceConstants.AUTOSAVE_ON_EXIT,
				Messages.GeneralPage_AutoSaveOnExit,
				getFieldEditorParent()));
		
		sectionLabel = new Label(getFieldEditorParent(), SWT.NONE);
		sectionLabel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1));
		
		sectionLabel = new Label(getFieldEditorParent(), SWT.NONE);
		sectionLabel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1));
		sectionLabel.setText(Messages.GeneralPage_SystemTraySectionTitle);
		
		mShowInTray = new BooleanFieldEditor(PreferenceConstants.TRAY_SHOW_TRAY_ICON,
				Messages.GeneralPage_ShowInTray,
				getFieldEditorParent());
		addField(mShowInTray);
		
		mMinimizeToTray = new BooleanFieldEditor(PreferenceConstants.TRAY_MINIMIZE_ON_CLOSE,
				Messages.GeneralPage_MinimizeToTray,
				getFieldEditorParent());
		addField(mMinimizeToTray);
	}

	@Override
	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub
		
	}

	
}
